package cn.iocoder.yudao.module.biz.controller.admin.trainingResult;

import cn.iocoder.yudao.module.biz.controller.admin.trainingRecord.vo.BizTrainingRecordPageJoinVO;
import cn.iocoder.yudao.module.biz.controller.admin.trainingRecord.vo.BizTrainingRecordPageReqVO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.biz.controller.admin.trainingResult.vo.*;
import cn.iocoder.yudao.module.biz.dal.dataobject.trainingResult.BizTrainingResultDO;
import cn.iocoder.yudao.module.biz.convert.trainingResult.BizTrainingResultConvert;
import cn.iocoder.yudao.module.biz.service.trainingResult.BizTrainingResultService;

@Api(tags = "管理后台 - 培训结果")
@RestController
@RequestMapping("/biz/training-result")
@Validated
public class BizTrainingResultController {

    @Resource
    private BizTrainingResultService trainingResultService;

    @PostMapping("/create")
    @ApiOperation("创建培训结果")
    @PreAuthorize("@ss.hasPermission('biz:training-result:create')")
    public CommonResult<String> createTrainingResult(@Valid @RequestBody BizTrainingResultCreateReqVO createReqVO) {
        return success(trainingResultService.createTrainingResult(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新培训结果")
    @PreAuthorize("@ss.hasPermission('biz:training-result:update')")
    public CommonResult<Boolean> updateTrainingResult(@Valid @RequestBody BizTrainingResultUpdateReqVO updateReqVO) {
        trainingResultService.updateTrainingResult(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除培训结果")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('biz:training-result:delete')")
    public CommonResult<Boolean> deleteTrainingResult(@RequestParam("id") String id) {
        trainingResultService.deleteTrainingResult(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得培训结果")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('biz:training-result:query')")
    public CommonResult<BizTrainingResultBaseVO> getTrainingResult(@RequestParam("id") String id) {
        BizTrainingResultPageReqVO vo=new BizTrainingResultPageReqVO();
        vo.setPageNo(0);
        vo.setPageSize(10);
        vo.setId(id);
        List<BizTrainingResultBaseVO> list = trainingResultService.selectTrainingResult(vo);
        BizTrainingResultBaseVO baseVO=list.get(0);
        return success(baseVO);
    }

    @GetMapping("/list")
    @ApiOperation("获得培训结果列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('biz:training-result:query')")
    public CommonResult<List<BizTrainingResultRespVO>> getTrainingResultList(@RequestParam("ids") Collection<String> ids) {
        List<BizTrainingResultDO> list = trainingResultService.getTrainingResultList(ids);
        return success(BizTrainingResultConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得培训结果分页")
    @PreAuthorize("@ss.hasPermission('biz:training-result:query')")
    public CommonResult<PageResult<BizTrainingResultRespVO>> getTrainingResultPage(@Valid BizTrainingResultPageReqVO pageVO) {
        PageResult<BizTrainingResultDO> pageResult = trainingResultService.getTrainingResultPage(pageVO);
        return success(BizTrainingResultConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping(value = "/selectTrainingResult")
    public PageResult<BizTrainingResultBaseVO> selectTrainingResult(BizTrainingResultPageReqVO pageVO) {
        PageResult<BizTrainingResultBaseVO> result=new PageResult<>();
        Integer pageNo=pageVO.getPageNo();
        Integer pageSize=  pageVO.getPageSize();
        //设置为limit开始的条数
        pageVO.setPageNo((pageNo-1)*pageSize);
        List<BizTrainingResultBaseVO> list = trainingResultService.selectTrainingResult(pageVO);
        Integer count= trainingResultService.selectTrainingResultCount(pageVO);
        result.setList(list);
        result.setTotal((long) count);
        return result;
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出培训结果 Excel")
    @PreAuthorize("@ss.hasPermission('biz:training-result:export')")
    @OperateLog(type = EXPORT)
    public void exportTrainingResultExcel(@Valid BizTrainingResultExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<BizTrainingResultDO> list = trainingResultService.getTrainingResultList(exportReqVO);
        // 导出 Excel
        List<BizTrainingResultExcelVO> datas = BizTrainingResultConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "培训结果.xls", "数据", BizTrainingResultExcelVO.class, datas);
    }

}
